package net.huashitong.Indicators.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.entity.ThreeIndex;
import net.huashitong.Indicators.filter.ThreeIndexFilter;
import net.huashitong.Indicators.requestModel.IndexRequestModel;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ThreeIndexDao extends StringDao<ThreeIndex> {
    /**
     * 通过codeId查询
     *
     * @param codeId
     * @return
     */
    public ThreeIndex queryByCodeId(String codeId) {
        GenericQuery query = createQuery("obj");
        query.append(" and obj.codeId=:codeId").setParam("codeId", codeId);
        return query.firstResult();
    }

    /**
     * 通过上级指标Code 查询
     *
     * @param parentCode
     * @return
     */
    public List<ThreeIndex> queryByParentCode(String parentCode) {
        GenericQuery query = createQuery("obj");
        query.append(" and obj.parentCode=:parentCode").setParam("parentCode", parentCode);
        return query.listResult();
    }

    public List<ThreeIndex> queryByThree(IndexRequestModel filter) {
        GenericQuery query = createQuery("obj");
        if(StringUtils.isNotBlank(filter.getCodeName()))
        query.append(" and obj.codeName=:codeName").setParam("codeName", filter.getCodeName());
        if(StringUtils.isNotBlank(filter.getAreaCode()))
        {
            query.append(" and obj.areaCode=:areaCode").setParam("areaCode",filter.getAreaCode());
        }
        return query.listResult();
    }

    /**
     * 查询（可多条件）
     *
     * @param filter
     * @return
     */
    public List<ThreeIndex> queryThreeIndex(ThreeIndexFilter filter) {
        GenericQuery query = createQuery("obj");
        //名称
        if (StringUtils.isNotBlank(filter.getCodeName())) {
            query.append(" and obj.codeName=:codeName").setParam("codeName", filter.getCodeName());
        }
        //地区
        if (StringUtils.isNotBlank(filter.getAreaCode())) {
            query.append(" and obj.areaCode=:areaCode").setParam("areaCode", filter.getAreaCode());
        }
        //节点ID
        if (StringUtils.isNotBlank(filter.getCodeId())) {
            query.append(" and obj.codeId=:codeId").setParam("codeId", filter.getCodeId());

        }
        //等级
        if (StringUtils.isNotBlank(filter.getRanks())) {
            query.append(" and obj.ranks=:ranks").setParam("ranks", filter.getRanks());

        }
        //时间
        if (StringUtils.isNotBlank(filter.getDatetime())) {
            query.append(" and obj.datetime=:datetime").setParam("datetime", filter.getDatetime());

        }
        //时间区间
        if (StringUtils.isNotBlank(filter.getBeginTime()) &&
                StringUtils.isNotBlank(filter.getEndTime())) {
            query.append(" and obj.datetime between " + filter.getBeginTime() + " and " + filter.getEndTime());

        }
        return query.listResult();
    }

    public List<ThreeIndex> getThreeIndexs(IndexRequestModel fitler)
    {

        GenericQuery query  = createQuery("obj");
        query.append(" and obj.areaCode=:areaCode ").setParam("areaCode",fitler.getAreaCode());
        query.append(" and obj.parentCode=:parentCode").setParam("parentCode",fitler.getParentCode());
        query.append(" and obj.datetime=:datetime").setParam("datetime",fitler.getDateTime());
        if(StringUtils.isNotBlank(fitler.getThreeIndexCode()))
        {
            query.append(" and obj.codeId=:codeId").setParam("codeId",fitler.getThreeIndexCode());
        }
        return  query.listResult();

    }

}
